import { ref, computed, onUnmounted } from 'vue'
import dayjs from 'dayjs'

export const useTimeCut = () => {
  const time = ref(0)
  const formatTime = computed(() => {
    return dayjs.unix(time.value).format('mm分ss秒')
  })

  let timer = 1
  const start = (t) => {
    time.value = t
    timer = setInterval(() => {
      time.value--
      if (time.value === 0) {
        clearInterval(timer)
      }
    }, 1000)
  }
  onUnmounted(() => {
    timer && clearInterval(timer)
  })
  return {
    formatTime,
    start
  }
}
